package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author lmk
 * @since 2023-11-08
 */
//@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
    private final RabbitMqHelper mqHelper;

    /**
     * 点赞增加和取消
     * @param recordFormDTO
     */
    @Override
    public void addDeleteLiked(LikeRecordFormDTO recordFormDTO) {
        //判断点赞还是取消点赞
        boolean success = recordFormDTO.getLiked() ? like(recordFormDTO) : unlike(recordFormDTO);
        //判断是否成功
        if (!success) {
            return;
        }
        Integer likeTimes = lambdaQuery().eq(LikedRecord::getBizId, recordFormDTO.getBizId()).count();
        // 发送MQ通知
        mqHelper.send(
                LIKE_RECORD_EXCHANGE,
                StringUtils.format(LIKED_TIMES_KEY_TEMPLATE, recordFormDTO.getBizType()),
                LikedTimesDTO.of(recordFormDTO.getBizId(), likeTimes));
    }

    /**
     * 查询点赞状态
     *
     * @param bizIds
     * @return
     */
    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        Long userId = UserContext.getUser();
        //查询点赞记录
        List<LikedRecord> list = lambdaQuery()
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, userId)
                .list();
        return list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());//收集
    }

    @Override
    public void readLikedTimesAndSendMessage(String bizType, int maxBizSize) {

    }

    //取消点赞
    private boolean unlike(LikeRecordFormDTO recordFormDTO) {
        return remove(new QueryWrapper<LikedRecord>().lambda()
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, recordFormDTO.getBizId()));
    }

    //点赞
    private boolean like(LikeRecordFormDTO recordFormDTO) {
        Long userId = UserContext.getUser();
        //查询点赞记录
        Integer count = lambdaQuery().eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, recordFormDTO.getBizId())
                .count();
        //判断是否点赞
        if (count > 0) {
            return false;
        }
        //没有直接新增
        LikedRecord r = new LikedRecord();
        r.setUserId(userId);
        r.setBizId(recordFormDTO.getBizId());
        r.setBizType(recordFormDTO.getBizType());
        return save(r);
    }
}
